package cn.blue.pet.controller;
import cn.blue.base.util.LoginContext;
import cn.blue.org.domain.Employee;
import cn.blue.org.service.EmployeeService;
import cn.blue.pet.domain.Pet;
import cn.blue.pet.domain.ProductDetail;
import cn.blue.pet.mapper.ProductDetailMapper;
import cn.blue.pet.service.IProductDetailService;
import cn.blue.pet.service.IProductService;
import cn.blue.pet.domain.Product;
import cn.blue.pet.query.ProductQuery;
import cn.blue.base.util.AjaxResult;
import cn.blue.base.util.PageList;
import cn.blue.pet.service.impl.ProductDetailServiceImpl;
import cn.blue.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;
    @Autowired
    private IProductDetailService productDetailService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product,HttpServletRequest request){
        //获取当前登陆人的店铺id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectBylogininfo(logininfo.getId());
        Long shopId = employee.getShopId();
        product.setShopId(shopId);
        try {
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.add(product);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id")Long id)
    {
        return productService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> list(){

        return productService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query,HttpServletRequest request)
    {   //获取当前登陆人的店铺id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectBylogininfo(logininfo.getId());
        Long shopId = employee.getShopId();
        query.setShopId(shopId);
        return productService.queryByPage(query );
    }
    //下架
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids) {
        productService.offsale(ids);
        return AjaxResult.me().success();
    }

    //上架
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids) {
        productService.onsale(ids);
        return AjaxResult.me().success();
    }


    @PostMapping("/dels")
    public AjaxResult buchdelete(@RequestBody List<Long> ids) {
        productService.batchDel(ids);
        return AjaxResult.me().success();
    }
    //根据宠物id查询详情
    @GetMapping("/productDetail/{id}")
    public Product selectproductDetail(@PathVariable("id")Long id)
    {
        return productService.productDetail(id);
    }
    //立即购买
    @PostMapping("/order")
    public AjaxResult byNow(@RequestBody Product product) {
        productService.byNow(product);
        return AjaxResult.me().success();
    }


}
